# -*- coding: utf-8 -*-

import sip
sip.setapi('QString', 2)
sip.setapi('QStringList', 2)
sip.setapi('QVariant', 2)

from PyQt4 import QtCore

#_______________________________________________________________________________
class CollectionTreeConfigController(QtCore.QObject):
    tagType_2_mpdTagType = {"Album": "album", 
                            "Artist": "artist", 
                            "Year": "date", 
                            "Genre": "genre",
                            "None": "title"}
    mpdTagType_2_tagType = {"album": "Album", 
                          "artist": "Artist", 
                          "date": "year", 
                          "genre": "Genre"}

    def __init__(self, server, model, view):
        QtCore.QObject.__init__(self)
        self.server_ctrl = server
        self.model = model
        self.view = view
        #signal
        self.server_ctrl.client_connected.connect(self.connect)
        self.view.conf_button.toggled.connect(self.set_frame_visible)
        
    def connect(self, connected):
        print("3")
        if connected:
            self.view.conf_button.setEnabled(True)
            for combobox in self.view.comboboxes:
                combobox.insertItems(0, self.view.combobox_items)
            self.view.comboboxes[0].activated.connect(self.on_level_0_activated)
            self.view.comboboxes[1].activated.connect(self.on_level_1_activated)
            self.view.comboboxes[2].activated.connect(self.on_level_2_activated)

    def set_frame_visible(self, visible):
        self.view.frame.setVisible(visible)
        if visible is True:
            for i in xrange(len(self.model.order_tagTypes)):
                text = self.mpdTagType_2_tagType[self.model.order_tagTypes[i]]
                self.view.set_current_text(i, text)
            for j in xrange(i + 1, 3):
                    text = "None"
                    self.view.set_current_text(j, text)
                    if j > i + 1:
                        self.view.comboboxes[j].setEnabled(False)

    def on_level_0_activated(self, index):
        text = str(self.view.comboboxes[0].itemText(index))
        if not self.model.order_tagTypes:
            cur_tag = "title"
        else:
            cur_tag = self.model.order_tagTypes[0]
        if self.tagType_2_mpdTagType[text] != cur_tag:
            if text == "None":
                self.view.set_current_text(1, "None")
                self.view.comboboxes[1].setEnabled(False)
                self.view.set_current_text(2, "None")
                self.view.comboboxes[2].setEnabled(False)
                for i in xrange(len(self.model.order_tagTypes)):
                    self.model.order_tagTypes.pop()
            else:
                if self.model.order_tagTypes:
                    self.model.order_tagTypes[0] = self.tagType_2_mpdTagType[text]
                else:
                    self.model.order_tagTypes.append(self.tagType_2_mpdTagType[text])
                self.view.comboboxes[1].setEnabled(True)
                self.view.comboboxes[2].setEnabled(True)
            self.model.insert_data()
    
    def on_level_1_activated(self, index):
        text = str(self.view.comboboxes[0].itemText(index))
        if len(self.model.order_tagTypes) <= 1:
            cur_tag = "title"
        else:
            cur_tag = self.model.order_tagTypes[1]
        if self.tagType_2_mpdTagType[text] != cur_tag:
            if text == "None":
                self.view.set_current_text(2, "None")
                self.view.comboboxes[2].setEnabled(False)
                for i in xrange(1, len(self.model.order_tagTypes)):
                    self.model.order_tagTypes.pop()
            else:
                if len(self.model.order_tagTypes) >= 2:
                    self.model.order_tagTypes[1] = self.tagType_2_mpdTagType[text]
                else:
                    self.model.order_tagTypes.append(self.tagType_2_mpdTagType[text])
                self.view.comboboxes[2].setEnabled(True)
            self.model.insert_data()
        
    def on_level_2_activated(self, index):
        text = str(self.view.comboboxes[0].itemText(index))
        if len(self.model.order_tagTypes) <= 2:
            cur_tag = "title"
        else:
            cur_tag = self.model.order_tagTypes[2]
        if self.tagType_2_mpdTagType[text] != cur_tag:       
            if text == "None":
                for i in xrange(2, len(self.model.order_tagTypes)):
                    self.model.order_tagTypes.pop()
            else:
                if len(self.model.order_tagTypes) == 3:
                    self.model.order_tagTypes[2] = self.tagType_2_mpdTagType[text]
                else:
                    self.model.order_tagTypes.append(self.tagType_2_mpdTagType[text])
            self.model.insert_data()   